package com.example.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.R;
import com.example.entity.Planet;

import java.util.List;

public class NormalBaseAdapter<T> extends BaseAdapter {

    private Context mContext;
    private List<T> mList;

    public NormalBaseAdapter(Context mContext, List<T> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            // 根据布局文件 item_  .xml 生成视图对象
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_base, null);
            viewHolder = new ViewHolder();
            // 为条目布局设置内容
            viewHolder.iv_icon = convertView.findViewById(R.id.iv_base_icon);
            viewHolder.tv_title = convertView.findViewById(R.id.tv_base_title);
            viewHolder.tv_content = convertView.findViewById(R.id.tv_base_content);
            // 将 ViewHolder 对象存储在 View 中（即将 ViewHolder 对象引用存储在 View 的 Tag 中）
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        Planet planet = (Planet) mList.get(position);
        viewHolder.iv_icon.setImageResource(planet.getIcon());
        viewHolder.tv_title.setText(planet.getTitle());
        viewHolder.tv_content.setText(planet.getContent());
        return convertView;
    }

    public final class ViewHolder {
        public ImageView iv_icon;
        public TextView tv_title;
        public TextView tv_content;
    }
}
